package com.zhijiang.hospital.service.impl;


import com.zhijiang.common.util.NucleicQuery;
import com.zhijiang.hospital.entrty.dto.LisTestRegDto;
import com.zhijiang.hospital.entrty.dto.NucleicCount;
import com.zhijiang.hospital.entrty.dto.NucleicDto;
import com.zhijiang.hospital.mapper.FuYouNucleicMapper;
import com.zhijiang.hospital.mapper.JiKongNucleicMapper;
import com.zhijiang.hospital.mapper.PeopleNucleicMapper;
import com.zhijiang.hospital.mapper.ZhongYiNucleicMapper;
import com.zhijiang.hospital.service.NucleicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 75936
 * @version 1.0
 * @project demo_hesuan
 * @description
 * @date 2022-12-14 09:33:48
 */
@Slf4j
@Service
public class NucleicServiceImpl implements NucleicService {
    @Resource
    PeopleNucleicMapper peopleNucleicMapper;
    @Resource
    FuYouNucleicMapper fuYouNucleicMapper;
    @Resource
    JiKongNucleicMapper jiKongNucleicMapper;
    @Resource
    ZhongYiNucleicMapper zhongYiNucleicMapper;
    @Override
    public NucleicDto findPeopleNucleicCount(NucleicQuery nucleicQuery) {
        NucleicDto nucleicDto = new NucleicDto();
        /**妇幼的数据**/
        Long fuYouNucleicCount = fuYouNucleicMapper.findPeopleNucleicCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        Long fuYouNucleicNegativeCount = fuYouNucleicMapper.findNucleicNegativeCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        Long fuyouNucleicPositiveCount = fuYouNucleicMapper.findNucleicPositiveCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        //NucleicDto fuYouNucleicCount = fuYouNucleicMapper.findPeopleNucleicCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        /**疾控的数据**/
        Long jiKongNucleicCount = jiKongNucleicMapper.findNucleicCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        Long jiKongNucleicNegativeCount = jiKongNucleicMapper.findNucleicNegativeCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        Long jiKongNucleicPositiveCount = jiKongNucleicMapper.findNucleicPositiveCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        //NucleicDto jiKongNucleicCount = jiKongNucleicMapper.findPeopleNucleicCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        /**中医的数据**/
       Long zhongYiNucleicCount = zhongYiNucleicMapper.findZhongYiNucleicCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
       Long zhongYiNucleicNegativeCount = zhongYiNucleicMapper.findZhongYiNucleicNegativeCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
       Long zhongYiNucleicPositiveCount = zhongYiNucleicMapper.findZhongYiNucleicPositiveCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        //NucleicDto zhongYiNucleicCount = jiKongNucleicMapper.findPeopleNucleicCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());

        Long peopleNucleicCount = peopleNucleicMapper.findPeopleNucleicCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        Long peoplenucleicPositiveCount = peopleNucleicMapper.findNucleicPositiveCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());
        Long peoplenucleicNegativeCount = peopleNucleicMapper.findNucleicNegativeCount(nucleicQuery.getStartTime(), nucleicQuery.getEndTime());



      nucleicDto.setNucleicCount(fuYouNucleicCount+jiKongNucleicCount+zhongYiNucleicCount+peopleNucleicCount);
      nucleicDto.setNucleicNegativeCount(fuYouNucleicNegativeCount+jiKongNucleicNegativeCount+zhongYiNucleicNegativeCount+peoplenucleicNegativeCount);
     nucleicDto.setNucleicPositiveCount(fuyouNucleicPositiveCount+jiKongNucleicPositiveCount+zhongYiNucleicPositiveCount+peoplenucleicPositiveCount);
        log.info(nucleicDto.toString());
        return nucleicDto;
    }

    @Override
    public List<LisTestRegDto> findPeopleNucleicData(NucleicQuery nucleicQuery) {

        //PageHelper.startPage(nucleicQuery.getPageNum(), nucleicQuery.getPageSize());
        List<LisTestRegDto>  list = peopleNucleicMapper.findPeopleNucleicData(nucleicQuery.getStartTime(), nucleicQuery.getEndTime(),nucleicQuery.getKeyWord());
        List<LisTestRegDto> fuYouList = fuYouNucleicMapper.findFuYouNucleicData(nucleicQuery.getStartTime(), nucleicQuery.getEndTime(),nucleicQuery.getKeyWord());
        List<LisTestRegDto> jiKongList =  jiKongNucleicMapper.findJiKongNucleicData(nucleicQuery.getStartTime(), nucleicQuery.getEndTime(),nucleicQuery.getKeyWord());
        List<LisTestRegDto> zhongYiList =  zhongYiNucleicMapper.findJiKongNucleicData(nucleicQuery.getStartTime(), nucleicQuery.getEndTime(),nucleicQuery.getKeyWord());
        list.addAll(fuYouList);
        list.addAll(jiKongList);
        list.addAll(zhongYiList);
        List<LisTestRegDto> collect = list.stream().
                sorted(Comparator.comparing(LisTestRegDto::getResultDate)).
                collect(Collectors.toList());

        //枝江市人民医院核酸码
        Map<String, List<LisTestRegDto>> map1 = collect.stream().collect(Collectors.groupingBy(LisTestRegDto::getBarcode));
        //省核酸码
        Map<String, List<LisTestRegDto>> map2 = collect.stream().filter(s->s.getMemo()!=null).collect(Collectors.groupingBy(LisTestRegDto::getMemo));
        //计算混检核酸
        collect.stream().forEach(s->{
            if(s.getMemo()==null || s.getMemo() == ""){
                List<LisTestRegDto> lisTestRegDtos = map1.get(s.getBarcode());
                s.setHybridNumber(lisTestRegDtos.size()+"混1");
            }else {
                List<LisTestRegDto> lisTestRegDtos = map2.get(s.getMemo());
                s.setBarcode(s.getMemo());
                s.setHybridNumber(lisTestRegDtos.size()+"混1");
            }
        });

        if (nucleicQuery.getKeyWord()!=null){
            if (nucleicQuery.getKeyWord().equals("阴性")){
                List<LisTestRegDto> yin = collect.stream().filter(s -> s.getResult().equals("阴性")).collect(Collectors.toList());
//     a
                return yin;
            }else if (!nucleicQuery.getKeyWord().equals("阴性")){
                List<LisTestRegDto> yang = collect.stream().filter(s -> !s.getResult().equals("阴性")).collect(Collectors.toList());
//                List<LisTestRegDto> countData = yang.stream().filter(s -> s.getHybridNumber().equals("1混1")).collect(Collectors.toList());
//                count.setSingle(countData.size());
//                count.setMany(yang.size()-countData.size());
                return yang;
            }
        }else {

            return collect;
        }

        return collect;
        //return collect;
    }

    @Override
    public NucleicCount findCount(NucleicQuery nucleicQuery) {
        NucleicCount data = new NucleicCount();
        List<LisTestRegDto>  list = peopleNucleicMapper.findPeopleNucleicData(nucleicQuery.getStartTime(), nucleicQuery.getEndTime(),nucleicQuery.getKeyWord());
        List<LisTestRegDto> fuYouList = fuYouNucleicMapper.findFuYouNucleicData(nucleicQuery.getStartTime(), nucleicQuery.getEndTime(),nucleicQuery.getKeyWord());
        List<LisTestRegDto> jiKongList =  jiKongNucleicMapper.findJiKongNucleicData(nucleicQuery.getStartTime(), nucleicQuery.getEndTime(),nucleicQuery.getKeyWord());
        List<LisTestRegDto> zhongYiList =  zhongYiNucleicMapper.findJiKongNucleicData(nucleicQuery.getStartTime(), nucleicQuery.getEndTime(),nucleicQuery.getKeyWord());
        list.addAll(fuYouList);
        list.addAll(jiKongList);
        list.addAll(zhongYiList);
        Map<String, List<LisTestRegDto>> map1 = list.stream().collect(Collectors.groupingBy(LisTestRegDto::getBarcode));
        Map<String, List<LisTestRegDto>> map2 = list.stream().filter(s -> s.getMemo() != null).collect(Collectors.groupingBy(LisTestRegDto::getMemo));
        list.stream().forEach(s->{
            if(s.getMemo()==null || s.getMemo() == ""){
                List<LisTestRegDto> lisTestRegDtos = map1.get(s.getBarcode());
                s.setHybridNumber(lisTestRegDtos.size()+"混1");
            }else {
                List<LisTestRegDto> lisTestRegDtos = map2.get(s.getMemo());
                s.setBarcode(s.getMemo());
                s.setHybridNumber(lisTestRegDtos.size()+"混1");
            }
        });

        List<LisTestRegDto> listDto = list.stream().filter(s -> s.getHybridNumber().equals("1混1")).collect(Collectors.toList());
        List<LisTestRegDto> listDto2 = list.stream().filter(s -> !s.getHybridNumber().equals("1混1")).collect(Collectors.toList());
        log.info(listDto.size()+"");
        data.setSingle(listDto.size()+"");

        Map<String, List<LisTestRegDto>> collect = list.stream().filter(s -> !s.getHybridNumber().equals("1混1")).collect(Collectors.groupingBy(LisTestRegDto::getBarcode));
        Map<String, List<LisTestRegDto>> collect1 = list.stream().filter(s -> !s.getHybridNumber().equals("1混1")).collect(Collectors.groupingBy(LisTestRegDto::getMemo));

        List<LisTestRegDto> collect22 = list.stream().filter(s -> !s.getHybridNumber().equals("1混1")).collect(Collectors.toList());
        log.info(collect22.size()+"");
        log.info(collect1.toString());
        data.setMany(collect.size()+collect1.size()+"");

        if(nucleicQuery.getKeyWord().equals("阴性")){
            List<LisTestRegDto> yin = list.stream().filter(s -> s.getResult().equals("阴性")).collect(Collectors.toList());
            log.info(yin.size()+"");
        }else {
            List<LisTestRegDto> yin = list.stream().filter(s -> !s.getResult().equals("阴性")).collect(Collectors.toList());
            log.info(yin.size()+"");
        }
        return data;
    }


}
